﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PookSistener.PO
{
    public partial class frmListAllSellOrder : Form
    {
        OleDbConnection conn = null;
        DataSet dataset = new DataSet();

        private bool isInit = true;

        public frmListAllSellOrder()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmListAllSellOrder_Load(object sender, EventArgs e)
        {
            conn = new OleDbConnection(PookSistener.Properties.Settings.Default.DatabaseConnectionString);
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }

            conn.Open();

            ListCustomers();
            ListAllSO();
            isInit = false;
        }

        private void ListAllSO()
        {
            if (comboBox1.Items.Count > 0)
            {
                dataGridView1.Rows.Clear();

                // MessageBox.Show("Item 0 : " + comboBox1.SelectedValue);
                int customer_id = Convert.ToInt32(comboBox1.SelectedValue);

                OleDbCommand cmd = new OleDbCommand("SELECT * FROM MyOrder WHERE CustomerId = @CustomerId AND OrderDate = @OrderDate", conn);
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@CustomerId", OleDbType.Integer).Value = customer_id;

                string today = dateTimePicker1.Value.Year.ToString("0000") + "" + dateTimePicker1.Value.Month.ToString("00") + "" + dateTimePicker1.Value.Day.ToString("00");

                cmd.Parameters.Add("@OrderDate", OleDbType.VarChar).Value = today;

                OleDbDataReader reader = cmd.ExecuteReader();

                List<int> OrderIdList = new List<int>();


                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        OrderIdList.Add(reader.GetInt32(0));
                    }
                }

                reader.Close();

                string inString = "";
                for (int i = 0; i < OrderIdList.Count; i++)
                {
                    inString += OrderIdList[i] + ",";
                }

                if (inString.Length > 0)
                {

                    cmd = new OleDbCommand("SELECT * FROM Product", conn);
                    cmd.Parameters.Clear();

                    reader = cmd.ExecuteReader();

                    Dictionary<string, string> Products = new Dictionary<string, string>();

                    while (reader.Read())
                    {
                        string product_id = reader.GetString(0);
                        string product_name = reader.GetString(1);
                        Products.Add(product_id, product_name);
                    }

                    reader.Close();


                    inString = inString.Substring(0, inString.Length - 1);

                    cmd = new OleDbCommand("SELECT * FROM OrderDetail WHERE OrderId IN (@OrderId)", conn);
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@OrderId", OleDbType.VarChar).Value = inString;

                    reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            dataGridView1.Rows.Add(new object[] { Products[reader.GetString(2)], reader.GetDouble(4), reader.GetDouble(3) });
                        }
                    }

                    reader.Close();
                }
               
            }
        }

        private void ListCustomers()
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Customer ORDER BY Name", conn);
            adapter.Fill(dataset, "Customer");

            comboBox1.DataSource = dataset.Tables["Customer"];
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "ID";
        }

        private void dataGridView1_Resize(object sender, EventArgs e)
        {
            int scrollBarWidth = SystemInformation.VerticalScrollBarWidth + 5;
            int rowHeaderWidth = dataGridView1.RowHeadersWidth + 3;

            int width = dataGridView1.Width - scrollBarWidth;

            dataGridView1.Columns["colProductName"].Width = (width - 300);
            dataGridView1.Columns["colPrice"].Width = (150);
            dataGridView1.Columns["colUnit"].Width = 150;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            ListAllSO();
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (!isInit)
            {
                ListAllSO();
            }
        }
    }
}
